package com.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.util.Transition;

public class Insert {
	
	/*
	 * return S001 => thanh cong
	 * return E001 => loi
	 * return E002 => San pham da duoc cap nhat
	 * return E003 => qua han khuyen mai
	 */
	public String insert(InfoProduct inf){
		try {
			new Transition().displayInfoProduct(inf);
			String result = "E001|Co loi phat sinh";			
			if(inf.SourceId==null) return "E001|Khong co id san pham";
			
			PreparedStatement pstmt = null;
		    Connection conn  = null;		    
			MysqlConnect mysql = new MysqlConnect();
			
		    try {
		    	//System.out.println("info:"+ inf.());
		    	String vc_sql=null;		    	
		    	int sourceId=0;
		    	conn = mysql.getConnection();		    	
			    conn.setAutoCommit(false);
			    //kiem tra san pham da duoc cap nhat chua?
			    vc_sql="SELECT id FROM  `product_temp` WHERE `source_id`=?";
			    pstmt = conn.prepareStatement(vc_sql);
			    pstmt.setString(1, inf.SourceId);
			    ResultSet rs = pstmt.executeQuery();
				while (rs.next())
					sourceId= rs.getInt("id");				
				//san pham da duoc cap nhat
				if(sourceId > 0) return "E002|"+inf.SourceId+" da duoc cap nhat";
			    
				//insert san pham to product_temp
				vc_sql ="INSERT INTO `product_temp` (`id`,`name`,`alias`,`keyword`,`type`,`content`,`price`,`price_sale`,`begin_date`,`end_date`,`city_id`,`img`,`ym`,`email`,`people`,`mobile`,`phone`,`address`,`source_id`,`source_url`,`code`,`summary`,`cat_id`,`created_by`,`created_date`) VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate())";				
				pstmt = conn.prepareStatement(vc_sql);
				
				pstmt.setString(1,inf.Name);
				pstmt.setString(2,inf.Alias);
				pstmt.setString(3,inf.Keyword);
				pstmt.setString(4,inf.Type);
				pstmt.setString(5,inf.Content);
				pstmt.setString(6,inf.Price);
				pstmt.setString(7,inf.PriceSale);
				pstmt.setString(8,inf.BeginDateSale);
				pstmt.setString(9,inf.EndDateSale);
				pstmt.setInt(10,Integer.parseInt(inf.City));
				pstmt.setString(11,inf.Img);
				pstmt.setString(12,inf.Ym);
				pstmt.setString(13,inf.Email);
				pstmt.setString(14,inf.People);
				pstmt.setString(15,inf.Mobile);
				pstmt.setString(16,inf.Phone);
				pstmt.setString(17,inf.Address);
				pstmt.setString(18,inf.SourceId);
				pstmt.setString(19,inf.SourceUrl);
				pstmt.setString(20,inf.Code);
				pstmt.setString(21,inf.Summary);
				pstmt.setString(22,inf.CatId);
				pstmt.setString(23,"1");
				
				int ext = pstmt.executeUpdate();
				
				
				if(ext>0){
					result="S001";
					conn.commit();
				}
				
				pstmt.close();
			    conn.close();
			} catch (Exception e) {
				System.err.println("Error: " + e.getMessage());
			    e.printStackTrace();	      
		    } finally {
		    	pstmt.close();
		    	conn.close();
		    }
			return result;
		} catch (Exception e) {
			System.out.println("loi "+e.toString());
			return "E001";
		}
	}
	
	
	
	public String insertCatProduct(String p_parent,String p_name, String p_keyword, String p_catid, String p_url){
		try {
			String result = "E001|Co loi phat sinh";
			PreparedStatement pstmt = null;
		    Connection conn  = null;
		    MysqlConnect mysql = new MysqlConnect();	
		    try {
		    	conn = mysql.getConnection();		    	
			    conn.setAutoCommit(false);
			    
			    String vc_sql="SELECT id FROM  `product_cat` WHERE `id_tkm`=?";
			    int id=0;
			    pstmt = conn.prepareStatement(vc_sql);
			    pstmt.setString(1, p_catid);
			    ResultSet rs = pstmt.executeQuery();
				while (rs.next())
					id= rs.getInt("id");				
				//san pham da duoc cap nhat
				if(id > 0) return "E002|Da ton tai category!";
				
		    	vc_sql ="INSERT INTO `product_cat` (`id`,`perent`,`name`,`keyword`,`id_tkm`,`url`) VALUES (NULL,?,?,?,?,?)";				
				pstmt = conn.prepareStatement(vc_sql);
				
				pstmt.setString(1,p_parent);
				pstmt.setString(2,p_name);
				pstmt.setString(3,p_keyword);
				pstmt.setString(4,p_catid);
				pstmt.setString(5,p_url);
				
				int ext = pstmt.executeUpdate();
				if(ext>0){
					result="S001";
					conn.commit();
				}
				pstmt.close();
			    conn.close();
			    
		    }catch (Exception e) {
				System.err.println("Error: " + e.getMessage());
			    e.printStackTrace();
			    return result;
		    } finally {
		    	pstmt.close();
		    	conn.close();
		    }
			return result;
		} catch (Exception e) {
			return "E001|Co loi phat sinh";
		}
	}
	
}
